﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_newslist : System.Web.UI.Page
    {
        private const int PAGESIZE = 10;
        public int pagecount = NewsInfoLogic.GetPageCount(PAGESIZE,0);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            int page = InitFromUrl("page",1);
            int category = InitFromUrl("cid",0);

            if(!IsPostBack){
                BindNewsList(page, category);
                noticesuccess.Visible = false;
                noticefailed.Visible = false;
            }
        }

        private void BindNewsList(int nowPage,int nowCat)
        {
            NewsListBindData(nowPage);
            NewsListPageBindData(nowPage);
            PagerLinkBindData(nowPage);
        }

        private void PagerLinkBindData(int nowPage)
        {
            int nextPage = nowPage + 1 > pagecount ? pagecount : nowPage + 1;
            int prePage = nowPage - 1 > 0 ? nowPage - 1 : 1;

            hlLast.NavigateUrl = "?page=" + pagecount.ToString();
            hlNext.NavigateUrl = "?page=" + nextPage.ToString();
            hlPre.NavigateUrl = "?page=" + prePage.ToString();
        }
        private void NewsListBindData(int nowPage)
        {
            List<NewsInfoList> newslist = new List<NewsInfoList>();
            List<NewsInfo> newsinfo = new List<NewsInfo>();
            newsinfo = NewsInfoLogic.GetAllNewsInfo(PAGESIZE, nowPage);//.GetPageNewsInfo();
            foreach (var ni in newsinfo)
            {
                NewsInfoList nl = (NewsInfoList)ni;
                nl.CategoryName = NewsCategoryInfoLogic.GetOneNewsCategoryInfo(ni.CategoryID).CategoryName;
                newslist.Add(nl);
            }
            NewsListRpt.DataSource = newslist;
            NewsListRpt.DataBind();
        }
        private void NewsListPageBindData(int nowPage)
        {
            List<int> page = new List<int>();
            for (int i = 1; i <= pagecount; i++)
            {
                //if (i != nowPage)
                page.Add(i);
            }
            NewsListPage.DataSource = page;
            NewsListPage.DataBind();
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (ddlChange.SelectedValue == "-1")
            {
                return;
            }
            if (Request.Form["ckb"]==null)
            {
                return;
            }

            //将获取到的newsid转换到一个数组里面
            string[] strNewsId = Request.Form["ckb"].Split(',');
            int[] intNewsId = new int[strNewsId.Count()];
            try
            {
                for (int i = 0; i < strNewsId.Count(); i++)
                {
                    intNewsId[i] = Int32.Parse(strNewsId[i]);
                }
            }
            catch
            {
                return;
            }

            string choose = ddlChange.SelectedValue;
            int isSuccess = -1;
            switch (choose)
            {
                case "1":
                    isSuccess = NewsInfoLogic.UpdateNewsInfoHotFlag(intNewsId, true);
                    break;
                case "2":
                    isSuccess = NewsInfoLogic.UpdateNewsInfoHotFlag(intNewsId, false);
                    break;
                case "3":
                    isSuccess = NewsInfoLogic.DeleteSomeNewsInfo(intNewsId);
                    break;
            }
            if (isSuccess != 1)
            {
                ChangeStatus(false);
            }
            else
            {
                ChangeStatus(true);
            }
            
            //
            int page = InitFromUrl("page", 1);
            int cid = InitFromUrl("url", 0);
            BindNewsList(page, cid);
        }

        protected void ibtnDelete_OnClick(object sender, EventArgs e)
        {
            ImageButton ib = new ImageButton();
            int NewsID = 0;
            try
            {
                ib = (ImageButton)sender;
                NewsID = Int32.Parse(ib.ToolTip.ToString());
            }
            catch {
                return;
            }
            if (NewsInfoLogic.DeleteOneNewsInfo(NewsID) != 1)
            {
                ChangeStatus(false);
            }
            else
            {
                ChangeStatus(true);
            }

            //
            int page = InitFromUrl("page", 1);
            int cid = InitFromUrl("url",0);
            BindNewsList(page, cid);
        }

        private int InitFromUrl(string url,int first)
        {
            int rtn = first;
            try
            {
                rtn = Int32.Parse(Request.QueryString[url].ToString());
            }
            catch { }
            return rtn;
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefailed.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }
    }
}
